library(logr)
log_open("log_A5_1.log")
log_code()
####################
#REPLICATION FILES: APPENDIX 5
#Article: "When Does Online Public Diplomacy Succeed? Evidence from China's ‘Wolf Warrior’ Diplomats"
#Authors: Daniel Mattingly and James Sundquist
#This Version: June 18, 2022


####################
#Description:
# This script produces Tables A2-A5 in the article.
# The code constructs composite outcomes using the method of principal components.
# Outcomes are constructed using pooled data, and separately for each wave.
# These outcomes are also used in A5_part_2.R to check whether results are sensitive to this decision.

#The script was written with R version 4.1.2
#It was created and tested on Mac OS X (11.6.5).

# Load packages
library(tidyverse)
library(xtable)
library(estimatr)
library(rio)

as.num <- function(x){
  as.numeric(as.character(x))
}

# Set working directory
setwd("~/Dropbox/Research/Diplomacy/Replication")

# load cleaned data
pooled <- import("twitter_diplomacy_data.csv")
pooled$Treatment <- factor(pooled$Treatment, levels = c("control", "prochina", "antius"))
pooled$t_ <- pooled$Treatment

pre <- pooled %>% filter(galwan == 0)
post <- pooled %>% filter(galwan == 1)

# China government
dv.names = c("Q21", "Q22", "Q25", "Q28", "Q29")
dv.text = c("The Chinese government is trustworthy.",
            "The Chinese government is dangerous.",
            "If China were to increase its military \n
            activities in the South China Sea,\n
            do you think it would be for\n
            offensive or defensive reasons?",
            "China's rise is good for India's economy",
            "China's rise is good for India's national security")
factor.obj = princomp(~ Q21+Q22+Q25+Q28+Q29, data=pooled, cor=TRUE, na.action = na.omit) # Principal component using all data

df <- as.data.frame(cbind(dv.text, as.numeric(round(factor.obj$loadings[,1], 2))))
colnames(df) <- c("Question", "PC loading (pooled)")

pre.obj = princomp(~ Q21+Q22+Q25+Q28+Q29, data=pre, cor=TRUE, na.action = na.omit) # Principal component using Wave 1 data
post.obj = princomp(~ Q21+Q22+Q25+Q28+Q29, data=post, cor=TRUE, na.action = na.omit) # Principal component using Wave 2 data

pre$china_gov_pca_waves = NA
pre$china_gov_pca_waves[complete.cases(pre[,dv.names])] = as.vector(pre.obj$scores[,1])
pre$china_gov_pca_waves <- scale(pre$china_gov_pca_waves)

post$china_gov_pca_waves = NA
post$china_gov_pca_waves[complete.cases(post[,dv.names])] = as.vector(post.obj$scores[,1])
post$china_gov_pca_waves <- scale(post$china_gov_pca_waves)

df1 <- as.data.frame(cbind(dv.text, as.numeric(round(pre.obj$loadings[,1], 2))))
df2 <- as.data.frame(as.numeric(round(post.obj$loadings[,1], 2)))
df.compare <- cbind(df1, df2)
colnames(df.compare) <- c("Question", "PC loading (pre)", "PC loading (post)")
df.compare <- left_join(df.compare, df, by = "Question")

mytable <- xtable(df.compare)
a2 <- print(mytable, include.rownames = FALSE)
write(a2, file = "TableA2.tex")


# China people
dv.names = c("Q17", "Q18", "Q24")
dv.text = c("It's good that Chinese ideas and customs are spreading here.",
            "Chinese culture has positive aspects.",
            "People from China are trustworthy.")
factor.obj = princomp(~ Q17+Q18+Q24, data=pooled, cor=TRUE, na.action = na.omit)

df <- as.data.frame(cbind(dv.text, as.numeric(round(factor.obj$loadings[,1], 2))))
colnames(df) <- c("Question", "PC loading (pooled)")

pre.obj = princomp(~ Q17+Q18+Q24, data=pre, cor=TRUE, na.action = na.omit)
post.obj = princomp(~ Q17+Q18+Q24, data=post, cor=TRUE, na.action = na.omit)

pre$china_people_pca_waves = NA
pre$china_people_pca_waves[complete.cases(pre[,dv.names])] = as.vector(pre.obj$scores[,1])
pre$china_people_pca_waves <- scale(pre$china_people_pca_waves)

post$china_people_pca_waves = NA
post$china_people_pca_waves[complete.cases(post[,dv.names])] = as.vector(post.obj$scores[,1])
post$china_people_pca_waves <- scale(post$china_people_pca_waves)


df1 <- as.data.frame(cbind(dv.text, as.numeric(round(pre.obj$loadings[,1], 2))))
df2 <- as.data.frame(as.numeric(round(post.obj$loadings[,1], 2)))
df.compare <- cbind(df1, df2)
colnames(df.compare) <- c("Question", "PC loading (pre)", "PC loading (post)")
df.compare <- left_join(df.compare, df, by = "Question")

mytable <- xtable(df.compare)
a3 <- print(mytable, include.rownames = FALSE)
write(a3, file = "TableA3.tex")

# China policy
dv.names = c("Q19","Q20","Q26_1","Q26_2","Q26_3","Q30")
dv.text = c("India should cooperate more with China on trade.",
            "India should cooperate more with China on national defense.",
            "The Indian government should publicly condemn the Chinese government for its actions in Xinjiang",
            "The Indian government should impose economic sanctions on the Chinese government for its actions in Xinjiang",
            "The Indian government should offer asylum to Uyghurs",
            "If India must chose between being allies with China or allies with the United States, it should pick China")
factor.obj = princomp(~Q19+Q20+Q26_1+Q26_2+Q26_3+Q30, data=pooled, cor=TRUE, na.action = na.omit)

df <- as.data.frame(cbind(dv.text, as.numeric(round(factor.obj$loadings[,1], 2))))
colnames(df) <- c("Question", "PC loading (pooled)")

pre.obj = princomp(~ Q19+Q20+Q26_1+Q26_2+Q26_3+Q30, data=pre, cor=TRUE, na.action = na.omit)
post.obj = princomp(~ Q19+Q20+Q26_1+Q26_2+Q26_3+Q30, data=post, cor=TRUE, na.action = na.omit)

pre$china_policy_pca_waves = NA
pre$china_policy_pca_waves[complete.cases(pre[,dv.names])] = as.vector(pre.obj$scores[,1])
pre$china_policy_pca_waves <- scale(pre$china_policy_pca_waves)

post$china_policy_pca_waves = NA
post$china_policy_pca_waves[complete.cases(post[,dv.names])] = as.vector(post.obj$scores[,1])
post$china_policy_pca_waves <- scale(post$china_policy_pca_waves)

df1 <- as.data.frame(cbind(dv.text, as.numeric(round(pre.obj$loadings[,1], 2))))
df2 <- as.data.frame(as.numeric(round(post.obj$loadings[,1], 2)))
df.compare <- cbind(df1, df2)
colnames(df.compare) <- c("Question", "PC loading (pre)", "PC loading (post)")
df.compare <- left_join(df.compare, df, by = "Question")

mytable <- xtable(df.compare)
a4 <- print(mytable, include.rownames = FALSE)
# Problems with all three Q26 questions
write(a4, file = "TableA4.tex")

# China COVID
dv.names = c("Q66", "Q67", "Q68")
dv.text = c("The Chinese government has done a good job of responding to the COVID-19 epidemic.",
            "The Chinese government is responsible for the COVID-19 epidemic.",
            "China has been generous in helping other countries during the COVID-19 epidemic.")
factor.obj = princomp(~ Q66+Q67+Q68, data=pooled, cor=TRUE, na.action = na.omit)

df <- as.data.frame(cbind(dv.text, as.numeric(round(factor.obj$loadings[,1], 2))))
colnames(df) <- c("Question", "PC loading (pooled)")

pre.obj = princomp(~ Q66+Q67+Q68, data=pre, cor=TRUE, na.action = na.omit)
post.obj = princomp(~ Q66+Q67+Q68, data=post, cor=TRUE, na.action = na.omit)

pre$china_covid_pca_waves = NA
pre$china_covid_pca_waves[complete.cases(pre[,dv.names])] = as.vector(pre.obj$scores[,1])
pre$china_covid_pca_waves <- scale(pre$china_covid_pca_waves)

post$china_covid_pca_waves = NA
post$china_covid_pca_waves[complete.cases(post[,dv.names])] = as.vector(post.obj$scores[,1])
post$china_covid_pca_waves <- scale(post$china_covid_pca_waves)

df1 <- as.data.frame(cbind(dv.text, as.numeric(round(pre.obj$loadings[,1], 2))))
df2 <- as.data.frame(as.numeric(round(post.obj$loadings[,1], 2)))
df.compare <- cbind(df1, df2)
colnames(df.compare) <- c("Question", "PC loading (pre)", "PC loading (post)")
df.compare <- left_join(df.compare, df, by = "Question")

mytable <- xtable(df.compare)
a5 <- print(mytable, include.rownames = FALSE)
write(a5, file = "TableA5.tex")

# US government
dv.names = c("Q35", "Q36", "Q40")
dv.text = c("The United States government is trustworthy.",
            "The United States government is dangerous.",
            "If the United States were to increase its military activities in the South China Sea, do you think it would be for offensive or defensive reasons?")
factor.obj = princomp(~ Q35+Q36+Q40, data=pooled, cor=TRUE, na.action = na.omit)

df <- as.data.frame(cbind(dv.text, as.numeric(round(factor.obj$loadings[,1], 2))))
colnames(df) <- c("Question", "PC loading (pooled)")

pre.obj = princomp(~ Q35+Q36+Q40, data=pre, cor=TRUE, na.action = na.omit)
post.obj = princomp(~ Q35+Q36+Q40, data=post, cor=TRUE, na.action = na.omit)

pre$usa_gov_pca_waves = NA
pre$usa_gov_pca_waves[complete.cases(pre[,dv.names])] = as.vector(pre.obj$scores[,1])
pre$usa_gov_pca_waves <- scale(pre$usa_gov_pca_waves)

post$usa_gov_pca_waves = NA
post$usa_gov_pca_waves[complete.cases(post[,dv.names])] = as.vector(post.obj$scores[,1])
post$usa_gov_pca_waves <- scale(post$usa_gov_pca_waves)

df1 <- as.data.frame(cbind(dv.text, as.numeric(round(pre.obj$loadings[,1], 2))))
df2 <- as.data.frame(as.numeric(round(post.obj$loadings[,1], 2)))
df.compare <- cbind(df1, df2)
colnames(df.compare) <- c("Question", "PC loading (pre)", "PC loading (post)")
df.compare <- left_join(df.compare, df, by = "Question")

mytable <- xtable(df.compare)
print(mytable, include.rownames = FALSE)

# US people
dv.names = c("Q31", "Q32", "Q38")
dv.text = c("It's good that American ideas and customs are spreading here.",
            "American culture has positive aspects.",
            "People from the United States are trustworthy.")
factor.obj = princomp(~ Q31+Q32+Q38, data=pooled, cor=TRUE, na.action = na.omit)

df <- as.data.frame(cbind(dv.text, as.numeric(round(factor.obj$loadings[,1], 2))))
colnames(df) <- c("Question", "PC loading (pooled)")

pre.obj = princomp(~ Q31+Q32+Q38, data=pre, cor=TRUE, na.action = na.omit)
post.obj = princomp(~ Q31+Q32+Q38, data=post, cor=TRUE, na.action = na.omit)

pre$usa_people_pca_waves = NA
pre$usa_people_pca_waves[complete.cases(pre[,dv.names])] = as.vector(pre.obj$scores[,1])
pre$usa_people_pca_waves <- scale(pre$usa_people_pca_waves)

post$usa_people_pca_waves = NA
post$usa_people_pca_waves[complete.cases(post[,dv.names])] = as.vector(post.obj$scores[,1])
post$usa_people_pca_waves <- scale(post$usa_people_pca_waves)

df1 <- as.data.frame(cbind(dv.text, as.numeric(round(pre.obj$loadings[,1], 2))))
df2 <- as.data.frame(as.numeric(round(post.obj$loadings[,1], 2)))
df.compare <- cbind(df1, df2)
colnames(df.compare) <- c("Question", "PC loading (pre)", "PC loading (post)")
df.compare <- left_join(df.compare, df, by = "Question")

mytable <- xtable(df.compare)
print(mytable, include.rownames = FALSE)


# US policy
dv.names = c("Q34", "Q33", "Q41_1", "Q41_2")
dv.text = c("India should cooperate more with the United States on trade",
            "India should cooperate more with the United States on national defense",
            "In January, the American government assassinated an Iranian general. The Indian government should publicly condemn the United States government for its actions.",
            "The Indian government should impose economic sanctions on the United States government.")
factor.obj = princomp(~ Q34+Q33+Q41_1+Q41_2, data=pooled, cor=TRUE, na.action = na.omit)

df <- as.data.frame(cbind(dv.text, as.numeric(round(factor.obj$loadings[,1], 2))))
colnames(df) <- c("Question", "PC loading (pooled)")

pre.obj = princomp(~ Q34+Q33+Q41_1+Q41_2, data=pre, cor=TRUE, na.action = na.omit)
post.obj = princomp(~ Q34+Q33+Q41_1+Q41_2, data=post, cor=TRUE, na.action = na.omit)

pre$usa_policy_pca_waves = NA
pre$usa_policy_pca_waves[complete.cases(pre[,dv.names])] = as.vector(pre.obj$scores[,1])
pre$usa_policy_pca_waves <- scale(pre$usa_policy_pca_waves)

post$usa_policy_pca_waves = NA
post$usa_policy_pca_waves[complete.cases(post[,dv.names])] = as.vector(post.obj$scores[,1])
post$usa_policy_pca_waves <- scale(post$usa_policy_pca_waves)

df1 <- as.data.frame(cbind(dv.text, as.numeric(round(pre.obj$loadings[,1], 2))))
df2 <- as.data.frame(as.numeric(round(post.obj$loadings[,1], 2)))
df.compare <- cbind(df1, df2)
colnames(df.compare) <- c("Question", "PC loading (pre)", "PC loading (post)")
df.compare <- left_join(df.compare, df, by = "Question")

mytable <- xtable(df.compare)
print(mytable, include.rownames = FALSE)

# US COVID
dv.names = c("Q69", "Q70")
dv.text = c("The United States government has done a good job of responding to the COVID-19 epidemic.",
            "The United States has been generous in helping other countries during the COVID-19 epidemic.")
factor.obj = princomp(~ Q69+Q70, data=pooled, cor=TRUE, na.action = na.omit)

df <- as.data.frame(cbind(dv.text, as.numeric(round(factor.obj$loadings[,1], 2))))
colnames(df) <- c("Question", "PC loading (pooled)")

pre.obj = princomp(~ Q69+Q70, data=pre, cor=TRUE, na.action = na.omit)
post.obj = princomp(~ Q69+Q70, data=post, cor=TRUE, na.action = na.omit)

pre$usa_covid_pca_waves = NA
pre$usa_covid_pca_waves[complete.cases(pre[,dv.names])] = as.vector(pre.obj$scores[,1])
pre$usa_covid_pca_waves <- scale(pre$usa_covid_pca_waves)

post$usa_covid_pca_waves = NA
post$usa_covid_pca_waves[complete.cases(post[,dv.names])] = as.vector(post.obj$scores[,1])
post$usa_covid_pca_waves <- scale(post$usa_covid_pca_waves)

df1 <- as.data.frame(cbind(dv.text, as.numeric(round(pre.obj$loadings[,1], 2))))
df2 <- as.data.frame(as.numeric(round(post.obj$loadings[,1], 2)))
df.compare <- cbind(df1, df2)
colnames(df.compare) <- c("Question", "PC loading (pre)", "PC loading (post)")
df.compare <- left_join(df.compare, df, by = "Question")

mytable <- xtable(df.compare)
print(mytable, include.rownames = FALSE)

#save(pre, file = "wave1_both_loadings.RData")
#save(post, file = "wave2_both_loadings.RData")

log_close()
